package com.zkthink.ceres.store.dao;

import com.baomidou.mybatisplus.annotation.SqlParser;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zkthink.base.mapper.SuperMapper;
import com.zkthink.ceres.store.dto.AllStoreDTO;
import com.zkthink.ceres.store.dto.CommentDto;
import com.zkthink.ceres.store.entity.Store;
import com.zkthink.ceres.store.vo.StoreShowVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * 店铺表
 * </p>
 *
 * @author kellen
 * @date 2020-05-14
 */
@Repository
public interface StoreMapper extends SuperMapper<Store> {

  @SqlParser(filter = true)
  public Store findByTenantCode(@Param("tenantCode") String tenantCode);

  @SqlParser(filter = true)
  List<Store> findStoreListIgnoreTenant(@Param("start")long start, @Param("pageSize")long size);

  @SqlParser(filter = true)
  int findStoreListIgnoreTenantCount();

  @SqlParser(filter = true)
  String findTenantCodeByStoreId(@Param("storeId") Long storeId);

  /**
   * 根据会员ID 查询店铺
   * @param mid
   */
  Store selectByMid(Long mid);

/*店详情
  */
  StoreShowVO xianqing(Long id);
  /**
   * APP店铺展示
   * @param dto
   * @return
   */
  List<StoreShowVO> allStore(@Param("dto") AllStoreDTO dto);
  /**
   * APP店铺展示评分排序
   * @param dto
   * @return
   */
  List<StoreShowVO> allStoreScore(@Param("dto") AllStoreDTO dto);

  /**
   * APP店铺展示评分排序
   * @param dto
   * @return
   */
  List<StoreShowVO> allStoreScoreHuaWei(@Param("dto") AllStoreDTO dto);

  /**
   * 店铺评分
   * @param storeId
   * @return
   */
  //@Select(" select IFNULL(ROUND(avg(desc_star),0),0) score from ceres_comment where store_id=#{storeId}")
  @Select(" select IFNULL( ROUND( avg( t.desc_star ), 0 ), 0 ) score from ( SELECT desc_star desc_star FROM ceres_comment WHERE store_id = #{storeId} union all select comment_star desc_star from applet_comment where store_id = #{storeId} ) t")
  int stotrComment(Long storeId);
  /**
   * 服务评分
   * @param productId
   * @return
   */
  @Select(" select  IFNULL(ROUND(avg(service_star),0),0)  score from ceres_comment where product_id=#{productId}")
  int productComment(Long productId);
  /**
   * 根据店铺id评论
   */
  List<CommentDto> comment(Long storeId);

  /**
   * 我的店
   * @param userType
   * @param mid
   * @return
   */
  Store myScore(@Param("userType") Integer userType,@Param("mid") Long mid);

  void add(Store store);

  /**
   * 今日营收
   * @param storeid
   * @return
   */
  BigDecimal today(@Param("storeid") Long storeid, @Param("s") String s);
  @Select("  select IFNULL( count(id),0) count  from chl_order where store_id = #{storeid} and  DATE_FORMAT(pay_time, '%Y-%m-%d') = #{s} and examine=1 and order_status !=4")
  int yuyue(@Param("storeid") Long storeid, @Param("s") String s);
  /**
   * 获取所有的市
   * @return
   */
   @Select("select city from ceres_store GROUP BY city")
  List<String> getcity();
  @Update("update ceres_store set frequency = frequency +1 where id = #{id}")
  int addFrequency(Long id);

  /**
   * 商品评论
   productId   * @return
   */
  List<CommentDto>  Projectcomment(Long productId);

    IPage<Store> IPageStore(IPage page,@Param("model") Store model);

   int deletStore (Long id);
}
